﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApplication5
{
    public class robot
    {
        public robot(int x, int y, ConsoleColor color)
        {
            _x = x;
            _y = y;
            _color = color;
        }
        public int _x;
        public int _y;
        public int _lastPrintedX;
        public int _lastPrintedY;
        public bool _hasBeenPrinted;
        public ConsoleColor _color;
        public int[,] s = new int[1,1];
        public List<int[,]> Memory = new List<int[,]>();
       
        public void SetPosition(int x, int y)
        {
            
        }

        public void MoveLeft(int interval)
        {
            _x -= interval;

            _x = Math.Max(_x, 0);
        }
        public void MoveRight(int interval)
        {
            _x += interval;

            _x = Math.Min(_x, Console.BufferWidth - 1);
        }
        public void MoveTop(int interval)
        {
            _y -= interval;

            _y = Math.Max(_y, 0);
        }
        public void MoveBottom(int interval)
        {
            _y += interval;

            _y = Math.Min(_y, Console.BufferHeight - 1);
        }
        public void PrintRobot()
        {
            if (_hasBeenPrinted)
            {
                //clear previous position
                Console.SetCursorPosition(_lastPrintedX, _lastPrintedY);
                Console.ResetColor();
                Console.Write("@");
            }
            Console.SetCursorPosition(_x, _y);

            Console.ForegroundColor = _color;
            Console.Write("#");
            _lastPrintedX = _x;
            _lastPrintedY = _y;
            
            
            _hasBeenPrinted = true;
        }
        public void DoStep()
        {
            int[,] s={{_x, _y}};

            Memory.Add(s);
            Random rnd = new Random();
            
            for (int i = 0; i < 1; i++ )
            {
                int[,] sl = { { _x - 1, _y } };
                int[,] sr = { { _x + 1, _y } };
                int[,] sb = { { _x, _y + 1 } };
                int[,] st = { { _x, _y - 1 } };
                int m = rnd.Next(0, 4);
                Thread.Sleep(100);
                if (m == 1)
                {
                    if (Memory.Contains(sl))
                    {
                        return;
                    }
                    else
                    {
                        MoveLeft(1);
                    }

                }
                else if (m == 2)
                {
                    if (Memory.Contains(sb))
                        return;
                    else
                    MoveBottom(1);

                }
                else if (m == 3)
                {
                    if (Memory.Contains(sr))
                        return;
                    else
                    MoveRight(1);

                }
                else if (Memory.Contains(st))
                {
                    return;
                }
                else
                {
                    MoveTop(1);

                }
                PrintRobot();
            }

        }
    }
}

